package com.zbkj.common.enums;

import cn.hutool.core.util.StrUtil;
import com.zbkj.common.model.admin.SystemAdmin;
import com.zbkj.common.utils.SecurityUtil;
import com.zbkj.common.vo.LoginUserVo;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 品牌商枚举
 * +----------------------------------------------------------------------
 * @author xiaolic
 * +----------------------------------------------------------------------
 */
public enum BrandEnumNew {
    // 致富包
    ZFB("zfb-front", "zfb-platform", "zfb-platform", 0),
    // 测试总平台
    TEST_PLATFORM("zfb-test.brand", "zfb-test-merchant", "zfb-test-admin", 0),
    ;
    // 前端地址
    private final String front;
    // 商户端地址
    private final String merchant;
    // 管理后台地址
    private final String admin;

    private final Integer id;

    BrandEnumNew(String front, String merchant, String admin, Integer id) {
        this.front = front;
        this.merchant = merchant;
        this.admin = admin;
        this.id = id;
    }

    public String getFront() {
        return front;
    }

    public String getMerchant() {
        return merchant;
    }

    public String getAdmin() {
        return admin;
    }

    /**
     * 根据url获取品牌商id
     * @param url
     * @return
     */
    public static Integer getId(String url) {
        for (BrandEnumNew typeEnum : BrandEnumNew.values()) {
            if (url.contains(typeEnum.getFront()) || url.contains(typeEnum.getMerchant()) || url.contains(typeEnum.getAdmin())) {
                return typeEnum.id;
            }
        }
        // 如果是本地直接获取idea vm环境变量中的值
        if (url.contains("localhost") || url.contains("127.0.0.1")) {
            // 先判断登陆是admin说明是总平台
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication.getPrincipal() instanceof LoginUserVo) {
                LoginUserVo principal = (LoginUserVo) authentication.getPrincipal();
                if (principal.getUser().getAccount().equals("admin")) {
                    return 0;
                }
            }
            String brandId = System.getProperty("BRANDID");
            if (StrUtil.isNotBlank(brandId)) {
                return Integer.parseInt(brandId);
            }
        }
        return 0;
    }

}
